package com.demo2do.fms.admin.entity.report.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import org.springframework.jdbc.core.RowMapper;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

/**
 *
 * @author Ark
 *
 */
public class TyreFeeReportSearch implements QueryBuilder {
    
    /** 运输业务公司 */
    private List<Long> organizationIds;
    
    /** 动力类型*/
    private Long powerTypeId;
    
    /** 公里数范围始 */
    private Integer rangeFrom;
    
    /** 公里数范围至 */
    private Integer rangeTo;

    /** 导出条数上限 */
    private Integer limit;
    
    private QueryOrder queryOrder;
    
    private QueryFilter queryFilter;

    /**
     * the default constructor
     */
    public TyreFeeReportSearch() {
        queryOrder = new QueryOrder().add("c", "organization_name", "ASC");
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {

        if (queryFilter == null || refresh) {

            queryFilter = new QueryFilter().addEquals("f", "power_type_id", powerTypeId)
                                           .addGreaterThanEqual("f", "range_from", rangeFrom)
                                           .addLessThanEqual("f", "range_to", rangeTo)
                                           .addSentence("f.organization_id IN (:organizationIds)", "organizationIds", organizationIds);
            
        }
        
        return queryFilter;
        
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        
        StringBuffer buffer = new StringBuffer(256);
        
        buffer.append("SELECT c.organization_name AS organizationName, ")
                     .append("b.vehicle_feature_code AS vehicleFeatureCode, ")
                     .append("b.vehicle_feature_name AS vehicleFeatureName, ")
                     .append("f.range_from AS rangeFrom, ")
                     .append("f.range_to AS rangeTo, ")
                     .append("f.price AS price ")
              .append("FROM tbl_truck_tyre_fee f, vw_transportation_company c, vw_vehicle_feature b ")
              .append("WHERE f.organization_id = c.id AND f.power_type_id = b.id")
              .append(getPreparedFilter(false).clauseSentence() + queryOrder.sentence());
        
        String sentence = "SELECT * FROM (" + buffer.toString() + ") WHERE ROWNUM <=" + limit;
        return sentence;
        
    }
    
    /**
     * build rowMapper
     * 
     * @return
     */
    public RowMapper<Map<String, Object>> buildRowMapper() {
        
        RowMapper<Map<String, Object>> rowMapper = new RowMapper<Map<String,Object>>() {
            
            /* (non-Javadoc)
             * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
             */
            public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
                
                Map<String, Object> result = new WeakHashMap<String, Object>();
                
                result.put("organizationName", rs.getObject("organizationName"));
                result.put("vehicleFeatureCode", rs.getObject("vehicleFeatureCode"));
                result.put("vehicleFeatureName", rs.getObject("vehicleFeatureName"));
                result.put("rangeFrom", rs.getObject("rangeFrom"));
                result.put("rangeTo", rs.getObject("rangeTo"));
                result.put("price", rs.getObject("price"));
                
                return result;
                
            }
            
        };
        
        return rowMapper;
        
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return queryOrder;
    }

    /**
     * @return the organizationIds
     */
    public List<Long> getOrganizationIds() {
        return organizationIds;
    }

    /**
     * @return the powerTypeId
     */
    public Long getPowerTypeId() {
        return powerTypeId;
    }

    /**
     * @return the rangeFrom
     */
    public Integer getRangeFrom() {
        return rangeFrom;
    }

    /**
     * @return the rangeTo
     */
    public Integer getRangeTo() {
        return rangeTo;
    }

    /**
     * @return the limit
     */
    public Integer getLimit() {
        return limit;
    }

    /**
     * @param organizationIds the organizationIds to set
     */
    public void setOrganizationIds(List<Long> organizationIds) {
        this.organizationIds = organizationIds;
    }

    /**
     * @param powerTypeId the powerTypeId to set
     */
    public void setPowerTypeId(Long powerTypeId) {
        this.powerTypeId = powerTypeId;
    }

    /**
     * @param rangeFrom the rangeFrom to set
     */
    public void setRangeFrom(Integer rangeFrom) {
        this.rangeFrom = rangeFrom;
    }

    /**
     * @param rangeTo the rangeTo to set
     */
    public void setRangeTo(Integer rangeTo) {
        this.rangeTo = rangeTo;
    }

    /**
     * @param limit the limit to set
     */
    public void setLimit(Integer limit) {
        this.limit = limit;
    }
    
}
